"""
# File       : image_001.py
# Time       ：2022/1/26 17:54
# Author     ：YuYe
"""
import time
import cv2
import numpy as np
import matplotlib.pyplot as plt

images = cv2.imread("../img/xiu.png", cv2.IMREAD_GRAYSCALE)
imagess = cv2.imread("../img/xiu.png")


def image_cv_show(
        name: str,
        img

):
    """
    读取图片
    :param name: 窗口名字
    :param img: 展示的图片
    :return: None
    """
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite("../img/imag/%s.png" % int(time.time()), img)


def mp_cv_show(
        mppath: str,
        vc_bame
):
    """
    学习opencv读取视频
    :param mppath: 视频路径
    :param vc_bame: 窗口名字
    :return: None
    """
    mp4_vc = cv2.VideoCapture(mppath)
    try:
        # 检查是否打开
        if mp4_vc.isOpened():
            oepn, frname = mp4_vc.read()
        else:
            oepn = False
    except:
        print("bug")
    finally:
        while oepn:
            ret, frname = mp4_vc.read()
            if frname is None:
                break
            if ret == True:
                gray = cv2.cvtColor(frname, cv2.COLOR_BGR2GRAY)
                cv2.imshow(vc_bame, gray)
                if cv2.waitKey(1) &0xFF == 27:
                    break
        mp4_vc.release()
        cv2.destroyAllWindows()


if __name__ == '__main__':
    image_cv_show("image", images)
    image_cv_show("img", imagess)
    mp_cv_show("../mp4/bug.mp4", "bug_mp4")
